/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ValidReceipt.java
 *
 * Created on 29-Mar-2011, 19:19:34
 */

package inventorymanagement;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import DataAccess.AccessDB;
import Entity.EReceipt;
import com.sun.rowset.CachedRowSetImpl;
import javax.swing.JOptionPane;

/**
 *
 * @author dung
 */
public class ValidReceipt extends javax.swing.JFrame {
DefaultTableModel MTableReceipt = new DefaultTableModel();
DefaultTableModel MTableItemReceipt = new DefaultTableModel();
AccessDB accessDb = new AccessDB();
    /** Creates new form ValidReceipt */
    public ValidReceipt() {
        initComponents();
        loadReceipt();
    }
    private void loadReceipt()
    {
        accessDb.LoadReceipt(MTableReceipt);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblReceiptList = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblItemReceipt = new javax.swing.JTable();
        btnAccept = new javax.swing.JButton();
        btnDecline = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.CardLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Receipt List", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(0, 102, 102))); // NOI18N

        tblReceiptList.setModel(MTableReceipt);
        tblReceiptList.getTableHeader().setReorderingAllowed(false);
        tblReceiptList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblReceiptListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblReceiptList);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 755, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Items Receipt list", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(0, 102, 102))); // NOI18N

        tblItemReceipt.setModel(MTableItemReceipt);
        tblItemReceipt.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tblItemReceipt);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 755, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
        );

        btnAccept.setText("Accept");
        btnAccept.setEnabled(false);
        btnAccept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAcceptActionPerformed(evt);
            }
        });

        btnDecline.setText("Decline");
        btnDecline.setEnabled(false);
        btnDecline.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeclineActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 777, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(385, Short.MAX_VALUE)
                .addComponent(btnAccept)
                .addGap(37, 37, 37)
                .addComponent(btnDecline)
                .addGap(42, 42, 42)
                .addComponent(btnClose)
                .addGap(122, 122, 122))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAccept)
                    .addComponent(btnDecline)
                    .addComponent(btnClose))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, "card2");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void tblReceiptListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblReceiptListMouseClicked
        // TODO add your handling code here:
        int iRows = tblReceiptList.getSelectedRow();
        EReceipt newReceipt = new EReceipt();
        newReceipt.setReceiptID(Integer.parseInt(String.valueOf(tblReceiptList.getValueAt(iRows, 0))));
        newReceipt.setBillID(Integer.parseInt(String.valueOf(tblReceiptList.getValueAt(iRows, 1))));
        String status = String.valueOf(tblReceiptList.getValueAt(iRows, 3));
        if(status.equalsIgnoreCase("true"))
        {
            btnAccept.setEnabled(false);
            btnDecline.setEnabled(false);
        }
        else if(status.equalsIgnoreCase("false"))
        {
            btnAccept.setEnabled(true);
            btnDecline.setEnabled(true);
        }
        accessDb.loadItemReceipt(newReceipt.getBillID(), MTableItemReceipt);
        
    }//GEN-LAST:event_tblReceiptListMouseClicked

    private void btnDeclineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeclineActionPerformed
       
            // TODO add your handling code here:
            int iRow = tblReceiptList.getSelectedRow();
            EReceipt newReceipt = new EReceipt();
            newReceipt.setReceiptID(Integer.parseInt(String.valueOf(tblReceiptList.getValueAt(iRow, 0))));
            try {
            CachedRowSetImpl rs = accessDb.crsi("update Receipt set valid=?,accept=? where ReceiptID =?");
            rs.setBoolean(1, true);
            rs.setBoolean(2, false);
            rs.setInt(3, newReceipt.getReceiptID());
            int nResult = JOptionPane.showConfirmDialog(null, "Are you sure to Decline this Receipt?", "Decline Receipt", JOptionPane.YES_NO_OPTION);
            if(nResult==JOptionPane.YES_OPTION)
                            {
                                rs.execute();
                                JOptionPane.showMessageDialog(null, "Receipt Declined", "Decline Receipt", JOptionPane.INFORMATION_MESSAGE);
                            }
        } catch (SQLException ex) {
            Logger.getLogger(ValidReceipt.class.getName()).log(Level.SEVERE, null, ex);
        }
        loadReceipt();
    }//GEN-LAST:event_btnDeclineActionPerformed

    private void btnAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAcceptActionPerformed
        // TODO add your handling code here:
        int iRows = tblItemReceipt.getRowCount();
        int nResult = JOptionPane.showConfirmDialog(null, "Are you sure to Accept this Receipt?", "Accept Receipt", JOptionPane.YES_NO_OPTION);
        if(nResult==JOptionPane.YES_OPTION)
                            {
        CachedRowSetImpl rs = accessDb.crsi("update Items set QuantityAvailable = QuantityAvailable+? where ItemName=?");
        for(int i=0;i<iRows;i++)
        {
            
            try {
                
                rs.setInt(1, Integer.parseInt(String.valueOf(tblItemReceipt.getValueAt(i, 3))));
                rs.setString(2, String.valueOf(tblItemReceipt.getValueAt(i, 1)));
                rs.execute();           
            } catch (SQLException ex) {
                Logger.getLogger(ValidReceipt.class.getName()).log(Level.SEVERE, null, ex);
            }
             }
                int iRow = tblReceiptList.getSelectedRow();
                EReceipt newReceipt = new EReceipt();
                newReceipt.setReceiptID(Integer.parseInt(String.valueOf(tblReceiptList.getValueAt(iRow, 0))));

                CachedRowSetImpl rsReceipt = accessDb.crsi("update Receipt set valid=?,accept=? where ReceiptID =?");
            try {
                rsReceipt.setBoolean(1, true);
                rsReceipt.setBoolean(2, true);
                rsReceipt.setInt(3, newReceipt.getReceiptID());
                rsReceipt.execute();
            } catch (SQLException ex) {
                Logger.getLogger(ValidReceipt.class.getName()).log(Level.SEVERE, null, ex);
            }
                
                JOptionPane.showMessageDialog(null, "Receipt Accepted", "Accept Receipt", JOptionPane.INFORMATION_MESSAGE);
             }
        loadReceipt();
    }//GEN-LAST:event_btnAcceptActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ValidReceipt().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAccept;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnDecline;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblItemReceipt;
    private javax.swing.JTable tblReceiptList;
    // End of variables declaration//GEN-END:variables

}
